﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NDepot.Models;
using NDepot.Repository;
using NDepot.Repository.Base;
using NDepot.Manager.Base;
using NDepot.Manager;
using NDepot.Models.UI;

namespace NDepot.Controllers
{
    [Authorize]
    public class ProductsController : DepotController
    {
        private IProductManager _ProductManager;

        public ProductsController() : this(ProductManager.GetInstance())
        {
        }

        public ProductsController(IProductManager ProductManager)
        {
            _ProductManager = ProductManager;
        }

        public ActionResult Index()
        {
            return View(_ProductManager.GetAll());
        }

        //
        // GET: /Ticket/Create

        public ActionResult Create()
        {
            return View(new ProductInput());
        }

        //
        // POST: /Ticket/Create

        [HttpPost]
        public ActionResult Create(ProductInput pi)
        {
            if (!_ProductManager.IsUniqueTitle(pi.Title))
                ModelState.AddModelError("Title", "Title has exists.");

            if (ModelState.IsValid)
            {
                _ProductManager.Insert(pi);

                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Ticket/Edit/5

        public ActionResult Edit(string id)
        {
            
            return View(_ProductManager.GetInputById(id));
        }

        //
        // POST: /Ticket/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, ProductInput pi)
        {
            if (ModelState.IsValid)
            {
                _ProductManager.Update(pi);

                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Ticket/Delete/5

        public ActionResult Delete(string id)
        {
            _ProductManager.Delete(id);

            return RedirectToAction("Index");
        }

        //
        // GET: /Ticket/Details/5

        public ActionResult Details(string id)
        {
            return View(_ProductManager.GetInputById(id));
        }
    }
}
